import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import('../components/Login')
  },
  {
    path: "/login/index",
    name: "idnex",
    redirect: '/login/index/blogs',
    meta: {requiredAuth: true},
    component: () => import('../components/Manager'),
    children: [{
      path: 'blogs',
      name: "blogs",
      meta: {requiredAuth: true},
      component: () => import('../views/admin/Blogs')
    },{
      path: 'addPage',
      name: 'addPage',
      meta: {requiredAuth: true},
      component: () => import('../views/admin/addPage')
    },{
      path: 'types',
      name: 'types',
      meta: {requiredAuth: true},
      component: () => import('../views/admin/Types')
    },{
      path: 'addTypes',
      name: 'addTypes',
      meta: {requiredAuth: true},
      component: () => import('../views/admin/addTypes')
    },{
      path: 'tags',
      name: 'tags',
      meta: {requiredAuth: true},
      component: () => import('../views/admin/Tags')
    },{
      path: 'addTags',
      name: 'addTags',
      meta: {requiredAuth: true},
      component: () => import('../views/admin/addTags')
    },{
      path: "friendLinks",
      name: 'friendLinks',
      meta: {requiredAuth: true},
      component: () => import('../views/admin/FriendLink')
    },{
      path: "comments",
      name: 'comments',
      meta: {requiredAuth: true},
      component: () => import('../views/admin/Comments')
    },{
      path: "addLink",
      name: "addLink",
      meta: {requiredAuth: true},
      component: () => import('../views/admin/addLink')
    },{
      path: 'setting',
      name: 'setting',
      meta: {requiredAuth: true},
      component: () => import('../views/admin/Setting')
    }]
  },
  {
    path: '/',
    name: "index",
    component: () => import("../components/Index")
  },
  {
    path: "/type",
    name: 'type',
    component: () => import("../components/Type")
  },
  {
    path: "/tag",
    name: 'tag',
    component: () => import("../components/Tag")
  },
  {
    path: "/archive",
    name: 'archive',
    component: () => import("../components/Archive")
  },{
    path: "/friendLink",
    name: 'friendLink',
    component: () => import("../components/FriendLink")
  },{
    path: '/page',
    name: 'page',
    component: () => import("../components/Page")
  },{
    path: '/error',
    name: 'error',
    component: () => import("../views/Error")
  }, {
    path: '/404',
    name: '404',
    component: () => import("../views/404")
  },{
    path: '/:pathMatch(.*)',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
//路由拦截
router.beforeEach( (to , form , next) => {
  if ( window.sessionStorage.user === undefined && to.name != 'Login' && to.meta.requiredAuth){next({name: 'Login'})}
  else {
    next()
  }
})

export default router
